home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktimeintro / play movie w.controller / start code / openmovieinwindow.c < prev    next >
Encoding:
Text File  |  2000-10-06  |  5.6 KB  |  190 lines

  1. // Play Movie with Controller Sample
  2. // Based on QTShell
  3. // WWDC 2000
  4.  
  5. #include "ComApplication.h"
  6. #include "ComFramework.h"
  7. #include "MacFramework.h"
  8.  
  9. //////////
  10. //
  11. // OpenMovieInWindow
  12. // Open a movie in a new movie window; return true if successful.
  13. //
  14. // This function is called from several places in our framework. The following combinations are possible:
  15. //    * theMovie == NULL, theFSSpec == NULL: no movie, no file; elicit a movie file from user and open it
  16. //    * theMovie != NULL, theFSSpec == NULL: new movie, no file (yet)
  17. //    * theMovie == NULL, theFSSpec != NULL: no movie, but we have an FSSpec; so just open the specified movie file
  18. //    * theMovie != NULL, theFSSpec != NULL: new movie, theFSSpec contains (at least) the movie name
  19. //
  20. //////////
  21.  
  22. Boolean OpenMovieInWindow (Movie theMovie, FSSpec *theFSSpec)
  23. {
  24.     WindowObject            myWindowObject = NULL;
  25.     Movie                    myMovie = NULL;
  26.     MovieController            myMC = NULL;
  27.     GraphicsImportComponent    myImporter = NULL;
  28.     WindowReference            myWindow = NULL;
  29.     FSSpec                    myFSSpec;
  30.     short                    myRefNum = kInvalidFileRefNum;
  31.     short                    myResID = 0;
  32.     OSType                     myTypeList[] = {kQTFileTypeMovie, kQTFileTypeQuickTimeImage};
  33.     short                    myNumTypes = 2;
  34.     GrafPtr                    mySavedPort;
  35.     Rect                    myRect = {0, 0, 0, 0};
  36.     Point                    myPoint;
  37.     QTFrameFileFilterUPP    myFileFilterUPP = NULL;
  38.     OSErr                    myErr = noErr;
  39.  
  40. #if TARGET_OS_MAC
  41.     myNumTypes = 0;
  42. #endif
  43.  
  44.     // get the current port; we may need to restore it if we cannot successfully create a new window
  45.     GetPort(&mySavedPort);
  46.     
  47.     // if we got neither a movie nor an FSSpec passed in, prompt the user for a movie file
  48.     if ((theMovie == NULL) && (theFSSpec == NULL)) {
  49.         myFileFilterUPP = QTFrame_GetFileFilterUPP((ProcPtr)QTFrame_FilterFiles);
  50.     
  51.         myErr = QTFrame_GetOneFileWithPreview(myNumTypes, (QTFrameTypeListPtr)myTypeList, &myFSSpec, myFileFilterUPP);
  52.     
  53.         if (myFileFilterUPP != NULL)
  54.             DisposeNavObjectFilterUPP(myFileFilterUPP);
  55.  
  56.         if (myErr != noErr)
  57.             goto bail;
  58.     }
  59.     
  60.     // if we got an FSSpec passed in, copy it into myFSSpec
  61.     if (theFSSpec != NULL) {
  62.         FSMakeFSSpec(theFSSpec->vRefNum, theFSSpec->parID, theFSSpec->name, &myFSSpec);        
  63.     }
  64.  
  65.     // if we got no movie passed in, read one from the specified file
  66.     if (theMovie == NULL) {
  67.  
  68.         // see if the FSSpec picks out an image file; if so, skip the movie-opening code
  69.         myErr = GetGraphicsImporterForFile(&myFSSpec, &myImporter);
  70.         if (myImporter != NULL)
  71.             goto gotImageFile;
  72.  
  73. // Step 1. Insert OpenMovieFile.clp here...
  74.  
  75.  
  76.         // if we couldn't open the file with even just read-only permission, bail....
  77.         if (myErr != noErr)
  78.             goto bail;
  79.  
  80.         // now fetch the first movie from the file
  81.         myResID = 0;
  82. // Step 2. Insert NewMovieFromFile.clp here...
  83.  
  84.         if (myErr != noErr)
  85.             goto bail;
  86.     } else {
  87.         myMovie = theMovie;
  88.     }
  89.  
  90.     //////////
  91.     //
  92.     // at this point, myMovie is an open movie, but myFSSpec may or may not be a valid FSSpec
  93.     //
  94.     //////////
  95.     
  96.     // set the default progress procedure for the movie
  97. // Step 3. Insert SetMovieProgressProc.clp here...
  98.         
  99. gotImageFile:
  100.     // create a new window to display the movie in
  101.     myWindow = QTFrame_CreateMovieWindow();
  102.     if (myWindow == NULL)
  103.         goto bail;
  104.     
  105.     myWindowObject = QTFrame_GetWindowObjectFromWindow(myWindow);
  106.     if (myWindowObject == NULL)
  107.         goto bail;
  108.     
  109.     // set the window title
  110.     QTFrame_SetWindowTitleFromFSSpec(myWindow, &myFSSpec, true);
  111.  
  112.     // make sure the movie or image file uses the window GWorld
  113.     if (myMovie != NULL)
  114. // Step 4. Insert SetMovieGWorld.clp here...
  115.  
  116.     if (myImporter != NULL)
  117.         GraphicsImportSetGWorld(myImporter, (CGrafPtr)QTFrame_GetPortFromWindowReference(myWindow), NULL);
  118.  
  119. // Step 5. Build SetupController.c
  120.  
  121.     // create and configure the movie controller
  122.     myMC = CreateController(myMovie, myWindow, true);
  123.         
  124.     // store movie info in the window record
  125.     (**myWindowObject).fMovie = myMovie;
  126.     (**myWindowObject).fController = myMC;
  127.     (**myWindowObject).fGraphicsImporter = myImporter;
  128.     (**myWindowObject).fFileResID = myResID;
  129.     (**myWindowObject).fFileRefNum = myRefNum;
  130.     (**myWindowObject).fCanResizeWindow = true;
  131.     (**myWindowObject).fIsDirty = false;
  132.     (**myWindowObject).fIsQTVRMovie = QTUtils_IsQTVRMovie(myMovie);
  133.     (**myWindowObject).fInstance = NULL;
  134.     (**myWindowObject).fAppData = NULL;
  135.     (**myWindowObject).fFileFSSpec = myFSSpec;
  136.     
  137.     // do any application-specific window object initialization
  138.     QTApp_SetupWindowObject(myWindowObject);
  139.     
  140.     // size the window to fit the movie and controller
  141.     QTFrame_SizeWindowToMovie(myWindowObject);
  142.  
  143.     // set the movie's play hints to allow dynamic resizing
  144.     SetMoviePlayHints(myMovie, hintsAllowDynamicResize, hintsAllowDynamicResize);
  145.  
  146.     // set the movie's position, if it has a 'WLOC' user data atom
  147.     myErr = QTUtils_GetWindowPositionFromFile(myMovie, &myPoint);
  148.  
  149.     // show the window
  150. #if TARGET_OS_MAC
  151.     MoveWindow(myWindow, myPoint.h, myPoint.v, false);
  152.     ShowWindow(myWindow);
  153.     SelectWindow(myWindow);                                // make it front-most, since it's just been created
  154.     InvalWindowRect(myWindow, GetWindowPortBounds(myWindow, &myRect));
  155. #endif
  156. #if TARGET_OS_WIN32
  157.     SetWindowPos(myWindow, 0, myPoint.h, myPoint.v, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
  158.     ShowWindow(myWindow, SW_SHOW);
  159.     UpdateWindow(myWindow);
  160. #endif
  161.  
  162.     // if the movie is a streamed movie, then start it playing immediately
  163.     if (QTUtils_IsStreamedMovie(myMovie))
  164. // Step 6. Insert MCDoAction.clp here...
  165.         
  166.     return(true);
  167.     
  168. bail:
  169.     if (myWindow != NULL)
  170. #if TARGET_OS_MAC
  171.         DisposeWindow(myWindow);
  172. #endif
  173. #if TARGET_OS_WIN32
  174.         SendMessage(ghWndMDIClient, WM_MDIDESTROY, (WPARAM)myWindow, 0L);
  175. #endif
  176.     
  177. // Step 7. Insert Dispose.clp here...    
  178.  
  179.         
  180.     if (myRefNum != 0)
  181.         CloseMovieFile(myRefNum);
  182.  
  183.     if (myImporter != NULL)
  184.         CloseComponent(myImporter);
  185.         
  186.     MacSetPort(mySavedPort);    // restore the port that was active when this function was called
  187.  
  188.     return(false);
  189. }
  190.